import { Storage } from './storage';

export type SessionData = {
  accessToken: string;
  refreshToken: string;
  expiresAt: number;
  userName: string;
}

const store = new Storage('@@session');

class Session {
  static clearSession = () => {
    return store.removeItem('s');
  }

  static setSession = (session: SessionData) => {
    return store.setItem('s', session);
  }

  static getSession = async () => {
    const result = await store.getItem<SessionData>('s');
    if (!result) {
      return;
    }
    const expired = Date.now() > result.expiresAt;
    return {
      ...result,
      expired
    }
  }
}

export default Session;

